/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeller.sound;

import java.io.File;
import jaco.mp3.player.MP3Player;

public class BackgroundMusic {
	MP3Player mp3Player;
	static BackgroundMusic INSTANCE = null;
	boolean isOn = true;


	public static BackgroundMusic getInstance() {
		if(INSTANCE==null)
			INSTANCE = new BackgroundMusic();
		return INSTANCE;
	}
	
	public BackgroundMusic()
	{
		mp3Player = null;
	}
	
	public void setMusic(String filename)
	{
		if(mp3Player!=null)
		{
			mp3Player.stop();
			mp3Player = null;
		}
		if(filename!=null && !filename.isEmpty())
		{
			try {
				mp3Player = new MP3Player(new File(filename));
				mp3Player.setRepeat(true);
			} catch (Exception e) {
				System.err.println("Exception in " + filename);
				e.printStackTrace();
			}
			if(mp3Player!=null && isOn)
			{
				mp3Player.play();
			}
		}
	}
	
	public void setOn(boolean on)
	{
		isOn = on;
		if(mp3Player!=null)
		{
			if(isOn)
			{
				mp3Player.play();
			}
			else
			{
				mp3Player.stop();
			}
		}
	}
	
	public boolean isOn()
	{
		return isOn;
	}

}
